﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq

'/// <summary> 
'/// Esta clase genera los formularios para mandar por post los datos a otras páginas
'/// </summary> 
Public Class RemotePost

    Dim _Inputs As System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection()
    Dim _Url As String = ""
    Dim _Method As String = "post"
    Dim _FormName As String = "form1"

    '/// <summary> 
    '/// Devuelve o setea la url  
    '/// </summary> 
    Public Property Url() As String
        Get
            Return _Url
        End Get
        Set(ByVal value As String)
            _Url = value
        End Set
    End Property

    '/// <summary> 
    '/// Agrega inputs hidden al formulario 
    '/// </summary> 
    '/// <param name="name"></param> 
    '/// <param name="value"></param> 
    Public Sub Add(ByVal name As String, ByVal value As String)
        _Inputs.Add(name, value)
    End Sub

    '/// <summary> 
    '/// Imprime en el cliente el formulario de compra 
    '/// </summary> 
    Public Sub Post()
        System.Web.HttpContext.Current.Response.Clear()
        System.Web.HttpContext.Current.Response.Write("")
        System.Web.HttpContext.Current.Response.Write(String.Format("<html><head></head><body onload=""document.{0}.submit()"">", _FormName))
        System.Web.HttpContext.Current.Response.Write(String.Format("<center><form target=""_blank"" name=""{0}"" method=""{1}"" action=""{2}"" >", _FormName, _Method, Url))


        For i As Int16 = 0 To _Inputs.Keys.Count - 1
            System.Web.HttpContext.Current.Response.Write(String.Format("<input name=""{0}"" type=""hidden"" value=""{1}"">", _Inputs.Keys(i), _Inputs(_Inputs.Keys(i))))
        Next
        System.Web.HttpContext.Current.Response.Write("</form></center></body></html>")
    End Sub


    'Como llamarlo (ej.: desde un boton)
    'Dim myremotepost As New RemotePost()
    '    myremotepost.Url = "PlanTrabajo.aspx"
    '    myremotepost.Add("Nuevo", "0")
    '    myremotepost.Add("Codigo", ViewState("VSCodigo"))
    '    myremotepost.Add("Periodo", ViewState("VSPeriodo"))
    '    myremotepost.Add("Estado", ViewState("VSEstado"))
    '    myremotepost.Add("Persona", ViewState("VSPersona"))
    '    myremotepost.Post()

    'Public Shared Function GetResponse_GET(ByVal urlBase As String, ByVal postData As Dictionary(Of String, String)) As String
    '    Dim primerParametro As Boolean = True
    '    Dim Parametros As StringBuilder = New StringBuilder()
    '    For Each param As KeyValuePair(Of String, String) In postData
    '        Parametros.Append(IIf(primerParametro = True, "?", "&")) 'Agregamos los separadores
    '        Parametros.Append(param.Key + "=" + System.Web.HttpUtility.UrlEncode(param.Value))
    '        primerParametro = False
    '    Next
    '    Dim urlparametros As String = urlBase + Parametros.ToString
    '    Dim wr As System.Net.WebRequest = System.Net.WebRequest.Create(urlparametros)
    '    wr.Method = "GET"
    '    wr.ContentType = "application/x-www-form-urlencoded"
    '    'Enviamos los parametros
    '    Dim nuevoStream As System.IO.Stream
    '    'Obtiene la respuesta
    '    Dim response As System.Net.WebResponse = wr.GetResponse()
    '    'Stream con el contenido recibido del servidor
    '    nuevoStream = response.GetResponseStream
    '    Dim reader As System.IO.StreamReader = New System.IO.StreamReader(nuevoStream)
    '    'Leemos el contenido
    '    Dim responseServidor As String = reader.ReadToEnd
    '    'Cerramos los Streams
    '    reader.Close()
    '    nuevoStream.Close()
    '    response.Close()
    '    Return responseServidor
    'End Function

End Class
